﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using fbasync.web.Data;

namespace fbasync.web.Controls.TemplateEditor
{
    public partial class ApplicationList : System.Web.UI.UserControl
    {
        private const string CookieKey = "TemplateEditorCurrentApplication";

        public static ApplicationList Instance
        {
            get { return (ApplicationList)HttpContext.Current.Items["ApplicationListInstance"]; }
            set { HttpContext.Current.Items["ApplicationListInstance"] = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            Instance = this;

            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack) InitApplicationList();
        }

        private void InitApplicationList()
        {
            uiApplicationList.DataTextField = "Name";
            uiApplicationList.DataValueField = "FacebookApiKey";
            uiApplicationList.DataSource = ApplicationManager.Applications;
            uiApplicationList.DataBind();

            foreach (ListItem item in uiApplicationList.Items)
            {
                if (ApplicationManager.IsCustomApplication(item.Value))
                {
                    item.Text = Global.GetString("UserApplicationListItem");
                }
            }

            string storedApiKey = ReadFromCookie();

            if (!String.IsNullOrEmpty(storedApiKey))
            {
                for (int itemIndex = 0; itemIndex < uiApplicationList.Items.Count; itemIndex++) 
                {
                    if (storedApiKey.Equals(uiApplicationList.Items[itemIndex].Value))
                    {
                        uiApplicationList.SelectedIndex = itemIndex;
                        break;
                    }
                }
            }

            SetCurrentApplication();
        }

        protected void ApplicationList_SelectedIndexChanged(object sender, EventArgs args)
        {
            SetCurrentApplication();
        }

        private void SetCurrentApplication()
        {
            if (uiApplicationList.SelectedItem == null) return;

            ApplicationManager.SetCurrentApplication(uiApplicationList.SelectedValue);

            if (ApplicationTemplates.Instance != null)
            {
                ApplicationTemplates.Instance.InitControls();
            }

            StoreToCookie();
        }

        private void StoreToCookie()
        {
            if (null == uiApplicationList.SelectedItem) return;

            if (Request.Browser.Cookies)
            {
                HttpCookie cookie = new HttpCookie(CookieKey, uiApplicationList.SelectedItem.Value);

                cookie.Expires = DateTime.Now.AddMonths(1);

                Response.Cookies.Add(cookie);
            }
        }

        private string ReadFromCookie()
        {
            if (!Request.Browser.Cookies) return String.Empty;

            HttpCookie cookie = Request.Cookies[CookieKey];

            return null == cookie ? String.Empty : cookie.Value;
        }

        protected void ApplyButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(uiApiKeyBox.Text) || String.IsNullOrEmpty(uiSecretKeyBox.Text))
            {
                ExceptionHandler.HandleException(new Exception(Global.GetString("errorEmptyAppValue")));
            }
            else
            {
                uiApplicationList.Items.Add(new ListItem(Global.GetString("UserApplicationListItem"), uiApiKeyBox.Text));

                ApplicationManager.AddCustomApplication(uiApiKeyBox.Text, uiSecretKeyBox.Text);

                ApplicationManager.SetCurrentApplication(uiApiKeyBox.Text);

                foreach (ListItem item in uiApplicationList.Items)
                {
                    if (item.Value.Equals(uiApiKeyBox.Text))
                    {
                        uiApplicationList.SelectedIndex = uiApplicationList.Items.IndexOf(item);
                        
                        break;
                    }
                }

                ApplicationTemplates.Instance.InitControls();
            }
        }
    }
}